package com.ruoyi.handler.utils;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 线程工具类
 *
 * @author wf
 */
public class ThreadPoolUtils {
    private ThreadPoolUtils() {
    }

    private static final ThreadPoolExecutor EXECUTOR = initExecutor();

    private static Integer CORE_POOL_SIZE = 10;

    private static Integer MAX_POOL_SIZE = 10;


    public static ThreadPoolExecutor initExecutor() {
        return new ThreadPoolExecutor(
                10,
                100,
                100L,
                TimeUnit.MICROSECONDS,
                new LinkedBlockingQueue<>());
    }

    public static void executeTask(Runnable runnable) {
        EXECUTOR.execute(runnable);
    }

    public static void shutdown() {
        EXECUTOR.shutdown();
    }

    public static void scheduledTask(Runnable task, long delay, TimeUnit unit) {
        ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(CORE_POOL_SIZE);

        scheduledThreadPoolExecutor.schedule(task, delay, unit);
    }

}
